﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TrabalhoAnaliseAlgoritmo.Metodos
{
    public static class ShiftAnd_Exato
    {
        public static Int64 DoShiftAndExato(String text, String pattern)
        {
            int textLength = text.Length;
            int patternLength = pattern.Length;
            var mask = new Int32[char.MaxValue];
            long R = 0, cont = 0;
            //pre-processamento
            for (int i = 0; i < textLength; i++)
            {
                mask[i] = 0;
            }

            for (int i = 1; i < patternLength; i++)
            {
                mask[pattern[i - 1] + 127] |= 1 << (patternLength - i);
            }

            //Busca
            for (int i = 0; i < textLength; i++)
            {
                R = ((R >> 1) | (1 << (patternLength - 1))) & mask[text[i] + 127];

                if ((R & 1) != 0)
                {
                    cont++;
                }
            }
            return cont;
        }
    }
}
